/**
 * 
 * @author vinicius.rodrigues
 */

package core.connector.email.http.ua;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

import core.connector.ConnectionDetails;
import core.connector.ConnectorType;
import core.connector.email.http.gmail.HttpConnectorGmail;
import core.util.JGoogleAnalytics;

/**
 *
 * @author Vinicius
 */
public class HttpConnectorUa extends HttpConnectorGmail{

    public HttpConnectorUa(ConnectionDetails connectionDetails){
        super(connectionDetails);
        int index;
        if(connectionDetails.getLogin() != null && ((index = connectionDetails.getLogin().trim().indexOf("@e-mail.ua")) > -1)){
            String login = connectionDetails.getLogin().substring(0,index);
            connectionDetails.setLogin(login);
            this.user = login;
        }
        JGoogleAnalytics.trackAsynchronously("Connector HTTP - E-MAIL.UA-MAIL");
    }

    @Override
    public boolean connect() {
        System.out.println("CONECTANDO...");
        if (user.equals("") || passwd.equals(""))
        {
            throw new IllegalStateException("Cannot connect: user and password not specified.");
        }
        PostMethod post = new PostMethod("http://e-mail.ua/login/");
        post.addRequestHeader("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png;q=0.5");
        post.addRequestHeader("Connection", "keep-alive");
        post.addRequestHeader("Accept-Encoding", "gzip, deflate");
        post.addRequestHeader("Referer", "http://e-mail.ua/");
        post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        post.addParameter("action", "login");
        post.addParameter("login", user);
        post.addParameter("passwd", passwd);
        post.addParameter("enter", "Войти");
        int statusCode = -1;
        String location = null;
        try {
	        post.setFollowRedirects(false);
	        statusCode = client.executeMethod(post);
	        Header[] setcookie = post.getResponseHeaders("set-cookie");
	        for(Header cok : setcookie)
	            buildCookie(cok);
	        if (statusCode == -1)
	            System.out.println("ERRO");
            Header hlocation = post.getResponseHeader("location"); 
            if(statusCode > 300 && hlocation != null){
            	location = hlocation.getValue();
            }            
	        if(GetCookie("euagusi") == null){
	            throw new Exception("Login/Senha incorreto(s)");
	        }
        }catch (Exception ex){
            ex.printStackTrace();
            post.releaseConnection();
            return false;
        }
        post.releaseConnection();
        if(location != null && location.endsWith(getServiceName())){
        	GetMethod get = new GetMethod(location);
        	get.addRequestHeader("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png;q=0.5");
        	get.addRequestHeader("Accept-Encoding", "gzip, deflate");
        	get.addRequestHeader("Referer", "http://e-mail.ua/");
        	get.addRequestHeader("Connection", "keep-alive");
        	setCookies(get);
        	
        	String resultado;
        	try {
        		statusCode = client.executeMethod(get);
        		resultado = getResponseBodyAsString(get);
    	        Header[] setcookie = post.getResponseHeaders("set-cookie");
    	        for(Header cok : setcookie)
    	            buildCookie(cok);
			} catch (Exception e) {
				e.printStackTrace();
	            get.releaseConnection();
	            return false;
			}
        	
        	if(resultado != null && resultado.contains("SAMLResponse")){
        		return super.acsConnect(resultado,"https://www.google.com/a/e-mail.ua/ServiceLogin?service=mail");
        	}
        	
        }
        return false;
    }

    @Override
    public String getServiceName(){
        return "a/e-mail.ua";
    }
    

}
